<?php
session_start();
include_once("functions/functions.php");

if (!testDB()) {
	die();
}
checkSSL();


if (isset($_GET['page'])) {
	if ($_GET['page'] == "submit_search") {
		saveSearchResults();
	}
}

//hides login unless specifically called.
$a->setShowLogin(false);
//sets each session to last for 10 hours.
$a->setExpire(36000);
//starts the authentication system.
$a->start();

/*print 'test 2';
exit();*/

include("inc/header.php");


		
$page = null;
if (isset($_GET['page'])) {
	$page = $_GET['page'];
}

if (isset($_GET['action'])) {
	if ($_GET['action'] == "logout" && $a->checkAuth()) {
		$a->logout();
		session_unregister('session_clientId');
		echo "<p>You have successfully logged out.</p>";
		echo "<p><a href=\"index.php\">Click here to return to the home page.</a></p>";
		//$a->start();
	}
}
elseif(!$a->getAuth()) {
	if ($page == "problem_add") {
		include_once("pages/problem_add.php");
	}
	elseif ($page == "submit_problem_add") {
		include_once("pages/submit_problem_add.php");
	}
	else {
?>
			<p class="heading" align="center"><br />Welcome to <? echo $orgName; ?>'s ERS</p>
			<div class="login">
				<table border="0">
					<tr>
						<td valign="top">
							<?
								if ($a->getStatus() == -3) {
									echo "<p align=\"left\" class=\"error\">Login was not successful, please try again.</p>";
								}
								loginFunction();
							?>
						</td>
					</tr>
				</table>
			</div>
			<p>
				<? echo $homepageHelpText; ?>
			</p>
<?
	}
}
else {
/*print 'page: ' . $page;
exit();*/
	if ($page == "submit_search") {
		include_once("pages/submit_search.php");
	}
	elseif ($page == "client_add") {
		include_once("pages/client_add.php");
	}
	elseif ($page == "client_view") {
		include_once("pages/client_view.php");
	}
	elseif ($page == "comment_view") {
		include_once("pages/comment_view.php");
	}
	elseif ($page == "reports") {
		include_once("pages/reports.php");
	}
	elseif ($page == "submit_client_add") {
		include_once("pages/submit_client_add.php");
	}
	elseif ($page == "visit_details") {
		include_once("pages/visit_details.php");
	}
	elseif ($page == "visit_add") {
		include_once("pages/visit_add.php");
	}
	elseif ($page == "submit_visit_add") {
		include_once("pages/submit_visit_add.php");
	}
	elseif ($page == "client_edit") {
		include_once("pages/client_edit.php");
	}
	elseif ($page == "submit_client_edit") {
		include_once("pages/submit_client_edit.php");
	}
	elseif ($page == "submit_visit_edit") {
		include_once("pages/submit_visit_edit.php");
	}
	elseif ($page == "visit_view_all") {
		include_once("pages/visit_view_all.php");
	}
	elseif ($page == "submit_visit_view_all") {
		include_once("pages/submit_visit_view_all.php");
	}
	elseif ($page == "comment_add") {
		include_once("pages/comment_add.php");
	}
	elseif ($page == "submit_comment_add") {
		include_once("pages/submit_comment_add.php");
	}
	elseif ($page == "visit_modify") {
		include_once("pages/visit_modify.php");
	}
	elseif ($page == "submit_visit_modify") {
		include_once("pages/submit_visit_modify.php");
	}
	elseif ($page == "search_advance") {
		include_once("pages/search_advance.php");
	}
	elseif ($page == "submit_search_advance") {
		include_once("pages/submit_search_advance.php");
	}
	elseif ($page == "problem_add") {
		include_once("pages/problem_add.php");
	}
	elseif ($page == "submit_problem_add") {
		include_once("pages/submit_problem_add.php");
	}
	elseif ($page == "page2") {
		include_once("pages/page2.php");
	}
	elseif ($page == "submit_page_2") {
		include_once("pages/submit_page_2.php");
	}
	elseif ($page == "page_2_button_modify") {
		include_once("pages/page_2_button_modify.php");
	}
	elseif ($page == "page_2_modify") {
		include_once("pages/page_2_modify.php");
	}
	elseif ($page == "submit_page2_modify") {
		include_once("pages/submit_page2_modify.php");
	}
	elseif ($page == "view_page_2") {
		include_once("pages/view_page_2.php");
	}
	else {
		include_once("pages/search.php");
	}

}

include("inc/footer.php");

?>